package com.example.WebTask1.Controller;

import com.example.WebTask1.Dao.ClassGradeDao;
import com.example.WebTask1.Dao.ClassGradeImp;
import com.example.WebTask1.Enity.ClassGrade;



import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ServletClassMessage", urlPatterns = "/ClassMessage")
public class ServletClassMessage extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String method = request.getParameter("method");
        if (method.equals("findAll")){
            this.queryClass(request,response);

        }else if (method.equals("insert")){
            this.insertClass(request,response);


        }else if (method.equals("updateFind")){
            this.updateFind(request,response);

        }else if (method.equals("updateClass")){
            this.updateClass(request,response);
        }else if (method.equals("delete")){
            this.deleteClass(request,response);
        }else {

        }
    }
    private void queryClass(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ClassGradeDao classGradeDao=new ClassGradeImp();
        List<ClassGrade> classGrades = classGradeDao.queryClass();
        request.setAttribute("classList",classGrades);
        request.getRequestDispatcher("classMessage.jsp").forward(request,response);

    }
    private void insertClass(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String aClass = request.getParameter("class");
        String grade = request.getParameter("grade");
        String chairman = request.getParameter("chairman");
        String slogan = request.getParameter("slogan");
        String count = request.getParameter("count");
        System.out.println(aClass+grade+chairman+slogan+count);
        ClassGrade classGrade = new ClassGrade();
        classGrade.setClassName(aClass);
        classGrade.setGrade(grade);
        classGrade.setChairman(chairman);
        classGrade.setSlogan(slogan);
        classGrade.setCount(Integer.parseInt(count));
        ClassGradeDao classGradeDao= new ClassGradeImp();
        Boolean aBoolean = classGradeDao.insertClass(classGrade);
        if (aBoolean){
            response.sendRedirect(request.getContextPath()+"/ClassMessage?method=findAll");
        }


    }

    private void deleteClass(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        ClassGrade classGrade = new ClassGrade();
        classGrade.setCid(id);
        ClassGradeDao classGradeDao =new ClassGradeImp();
        Boolean aBoolean = classGradeDao.deleteClass(classGrade);
        if (aBoolean){
            response.sendRedirect(request.getContextPath()+"/ClassMessage?method=findAll");
        }

    }

    private void updateClass(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String cid=request.getParameter("cid");
        String aClass = request.getParameter("class");
        String grade = request.getParameter("grade");
        String chairman = request.getParameter("chairman");
        String slogan = request.getParameter("slogan");
        String count = request.getParameter("count");
        System.out.println(aClass+grade+chairman+slogan+count);
        ClassGrade classGrade = new ClassGrade();
        classGrade.setCid(Integer.parseInt(cid));
        classGrade.setClassName(aClass);
        classGrade.setGrade(grade);
        classGrade.setChairman(chairman);
        classGrade.setSlogan(slogan);
        classGrade.setCount(Integer.parseInt(count));
        System.out.println("11111");
        ClassGradeDao classGradeDao=new ClassGradeImp();
        Boolean aBoolean = classGradeDao.updateClass(classGrade);
        if (aBoolean){
            response.sendRedirect(request.getContextPath()+"/ClassMessage?method=findAll");
        }


    }

    private void updateFind(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cid=request.getParameter("cid");
        String aClass = request.getParameter("className");
        String grade = request.getParameter("grade");
        String chairman = request.getParameter("chairman");
        String slogan = request.getParameter("slogan");
        String count = request.getParameter("count");
        System.out.println(aClass+grade+chairman+slogan+count);
        ClassGrade classGrade = new ClassGrade();
        classGrade.setCid(Integer.parseInt(cid));
        classGrade.setClassName(aClass);
        classGrade.setGrade(grade);
        classGrade.setChairman(chairman);
        classGrade.setSlogan(slogan);
        classGrade.setCount(Integer.parseInt(count));
        request.setAttribute("classGrade",classGrade);
        request.getRequestDispatcher("classUpdateMessage.jsp").forward(request,response);
    }



}
